//
//  YCSelectClassStyleViewController.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/1.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  选择班型

#import "YCSelectClassStyleViewController.h"
#import "YCSelectTrainMethodViewController.h"
#import "YCClassStyleCell.h"
#import "YCClassStyleModel.h"

@interface YCSelectClassStyleViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;

/** 底部条  */
@property (nonatomic, strong) UIView *bottomView;
/** 费用标签  */
@property (nonatomic, strong) UILabel *costLabel;
/** 下一步  */
@property (nonatomic, strong) UIButton *nextStep;
/** 数据  */
@property (nonatomic, strong) NSArray *dataArray;
/** 填写资料的模型  */
@property (nonatomic, strong) YCFillInfomationModel *fillInfoModel;
@end

@implementation YCSelectClassStyleViewController

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.yc_width, self.view.yc_height-64-50) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    }
    return _tableView;
}



// 底部条
- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        UIButton *nextStep = [[UIButton alloc] init];
        [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
        [nextStep setTitleColor:[UIColor yc_colorWithUInt:0x4766ab] forState:UIControlStateNormal];
        [nextStep setTitleColor:[UIColor yc_colorWithUInt:0xa1a1a1] forState:UIControlStateDisabled];
        nextStep.titleLabel.font = [UIFont systemFontOfSize:16];
        nextStep.enabled = NO;//默认无效
        [nextStep addTarget:self action:@selector(didTouchUpInsideNextStepButton:) forControlEvents:UIControlEventTouchUpInside];
        self.nextStep = nextStep;
        
        UILabel *costLabel = [[UILabel alloc] init];
        costLabel.font = [UIFont systemFontOfSize:14];
        costLabel.textColor = [UIColor yc_colorWithUInt:0xf14005];
        costLabel.text = @"费用合计:¥9800";
        self.costLabel = costLabel;
        
        [_bottomView addSubview:nextStep];
        [_bottomView addSubview:costLabel];
        
        [nextStep mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bottomView).offset(-33);
            make.centerY.equalTo(_bottomView);
        }];
        [costLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bottomView).offset(21);
            make.centerY.equalTo(_bottomView);
        }];
    }
    return _bottomView;
}

#pragma mark - 生命周期

- (id)initWithFillInfoModel:(YCFillInfomationModel *)model
{
    if (self = [super init]) {
        _fillInfoModel = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择班型";
    
    [self yc_addBackBarButtonItem];
    self.tableView.contentInset = UIEdgeInsetsMake(12, 0, 0, 0);//18 = 12 + 6
    self.tableView.rowHeight = heightFromIphone6Height(135);// 123+间距12(上下各6)
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
    
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    [self.tableView.mj_header beginRefreshing];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 导航条透明
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setShadowImage:[UIImage yc_imageWithColor:[UIColor clearColor] size:CGSizeMake(kScreenWidth, 1)]];
    [bar setBackgroundImage:[UIImage yc_imageWithColor:[UIColor yc_colorWithUInt:0xf7f7f7] size:CGSizeMake(kScreenWidth, 64)] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 还原导航条背景
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setShadowImage:[UIImage yc_imageWithColor:[UIColor clearColor] size:CGSizeMake(kScreenWidth, 1)]];
    [bar setBackgroundImage:[UIImage yc_imageWithColor:[UIColor whiteColor] size:CGSizeMake(kScreenWidth, 64)] forBarMetrics:UIBarMetricsDefault];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 数据处理
- (void)loadData
{
    //
    if(!self.fillInfoModel.cartypeid)
    {
        [SVProgressHUD showErrorWithStatus:@"请先选择车型"];
        return;
    }
    
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             @"cartypeid":@(self.fillInfoModel.cartypeid),
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPISelectClassType params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        self.dataArray = [YCClassStyleModel yc_objectArrayWithKeyValuesArray:response.data];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [self.tableView.mj_header endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCWeakSelf
    YCClassStyleCell *cell = [YCClassStyleCell tableViewCellWithTableView:tableView withIndexPath:indexPath detailButtonActionBlock:^{
        // 点击详情介绍按钮处理处
        YCClassStyleModel *mode = weakSelf.dataArray[indexPath.row];
        YCLog(@"是否选中:%@",mode.title);
    }];
    cell.contentView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    cell.itemModel = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCClassStyleModel *model = self.dataArray[indexPath.row];
    for (YCClassStyleModel *otherModel in self.dataArray) {
        if (otherModel != model) {
            otherModel.selected = NO;
        }
    }
    model.selected = !model.isSelected;
    [tableView reloadData];
    
    // 遍历所有,如果没一个选中,那么就下步一无效
    for (YCClassStyleModel *model in self.dataArray) {
        if (model.isSelected) {
            self.nextStep.enabled = YES;//下一步有效
            self.fillInfoModel.classtypeid = model.classtypeid;
            self.fillInfoModel.classtypeName = model.title;
            break;
        } else {
            self.nextStep.enabled = NO;
        }
    }
}


#pragma mark - 点击事件
- (void)didTouchUpInsideNextStepButton:(UIButton *)button
{
    YCLog(@"%s",__func__);

    YCSelectTrainMethodViewController *trainMethodVC = [[YCSelectTrainMethodViewController alloc] initWithFillInfoModel:self.fillInfoModel];
    [self.navigationController pushViewController:trainMethodVC animated:YES];
}


@end
